package minh.bac.wcbrazil2014.adapter;

import java.util.List;
import java.util.TreeSet;

import minh.bac.wcbrazil2014.R;
import minh.bac.wcbrazil2014.object.SlidingMenuObject;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

//public class MenuAdapter extends ArrayAdapter<SlidingMenuObject> {
//
//	Context context;
//	int resource;
//	List<SlidingMenuObject> object;
//
//	public MenuAdapter(Context context, int resource,
//			List<SlidingMenuObject> objects) {
//		super(context, resource, objects);
//		// TODO Auto-generated constructor stub
//		this.context = context;
//		this.resource = resource;
//		this.object = objects;
//	}
//
//	@Override
//	public View getView(int position, View convertView, ViewGroup parent) {
//		// TODO Auto-generated method stub
//		ViewHolder holder;
//		LayoutInflater inflater = (LayoutInflater) context
//				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//		if (convertView == null) {
//			holder = new ViewHolder();
//
//			convertView = inflater.inflate(resource, parent, false);
//			holder.imgeIcon=(ImageView)convertView.findViewById(R.id.imageIcon);
//			holder.textTitle=(TextView)convertView.findViewById(R.id.textTitleMenu);
//			convertView.setTag(holder);
//		}
//		else
//			holder = (ViewHolder)convertView.getTag();
//		
//		holder.imgeIcon.setImageDrawable(context.getResources().getDrawable(object.get(position).getIdicon()));
//		holder.textTitle.setText(object.get(position).gettitle());
//		
//		return convertView;
//
//	}
//
//	private class ViewHolder {
//		ImageView imgeIcon;
//		TextView textTitle;
//	}
//}
public class MenuAdapter extends BaseAdapter{
	
	public static final int TYPE_ITEM_NORMAL = 0;
	public static final int TYPE_ITEM_SPECIAL = 1;
	public static final int TYPE_ITEM_NEWS = 2;
	public static final int TYPE_VIEW_COUNT = TYPE_ITEM_NEWS+1;
	Context context; 	
	List<SlidingMenuObject> objects;
	//private TreeSet<Integer> mtrTreeSet = new TreeSet<>();
	private LayoutInflater inflater;
	
	public MenuAdapter(Context context, int resource,
			List<SlidingMenuObject> objects){
		this.context = context;
		this.objects = objects;
		inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return objects.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return objects.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}
	@Override
	public int getViewTypeCount() {
		// TODO Auto-generated method stub
		return TYPE_VIEW_COUNT;
	}
	@Override
	public int getItemViewType(int position) {
		// TODO Auto-generated method stub
		switch (position) {
		case 1:
			return TYPE_ITEM_SPECIAL;
			//break;
		case 2:
		case 3: 
		case 4:		
			return TYPE_ITEM_NEWS;
		default:
			return TYPE_ITEM_NORMAL;
		}
		//return super.getItemViewType(position);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		int type = getItemViewType(position);
		ViewHolder holderView = null;
		if(convertView == null)
		{
			
			switch (type) {
			case TYPE_ITEM_SPECIAL:
				holderView = new ViewHolder();
				convertView = inflater.inflate(R.layout.menu_item_two, parent, false);
				holderView.menuTitle = (TextView) convertView.findViewById(R.id.title_item_menu);
				holderView.icon = (ImageView) convertView.findViewById(R.id.imageIcon_news);
				break;
			case TYPE_ITEM_NEWS:
				holderView = new ViewHolder();
				convertView = inflater.inflate(R.layout.menu_item_news, parent, false);
				holderView.menuTitle = (TextView) convertView.findViewById(R.id.item_menu_title_news);
			
				break;
			case TYPE_ITEM_NORMAL:
				holderView = new ViewHolder();
				convertView = inflater.inflate(R.layout.menu_item, parent, false);
				holderView.menuTitle = (TextView) convertView.findViewById(R.id.textTitleMenu);
				holderView.icon = (ImageView) convertView.findViewById(R.id.imageIcon);
			default :
				
				break;
				
				
			}
			convertView.setTag(holderView);			
		}
		else
			holderView = (ViewHolder) convertView.getTag();
		if(type == TYPE_ITEM_NORMAL || type ==  TYPE_ITEM_SPECIAL)
		{
			holderView.icon.setImageDrawable(context.getResources().getDrawable(objects.get(position).getIdicon()));
			holderView.menuTitle.setText(objects.get(position).gettitle());
		}
		else
		{
			holderView.menuTitle.setText(objects.get(position).gettitle());
		}
		
		return convertView;
	}
	private class ViewHolder{
		TextView menuTitle;
		ImageView icon;
	}
}
